
package intraverbal;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class cadastrarPalavras extends javax.swing.JFrame {


    public cadastrarPalavras() {
        initComponents();
        txtIdioma1.setToolTipText("Palavra que vai aparecer ao usuário");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtIdioma1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtIdioma2 = new javax.swing.JTextField();
        btnCadastrar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        comboFase = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Idioma 1");

        jLabel2.setText("Idioma 2");

        btnCadastrar.setText("Cadastrar Palavras");
        btnCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCadastrarActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setText("Cadastro de Palavras");

        jLabel4.setText("Fase");

        comboFase.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Fase 1", "Fase 2", "Fase 3" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(289, 289, 289)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtIdioma1, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnCadastrar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtIdioma2, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)
                            .addComponent(comboFase, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap(303, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(jLabel3)
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtIdioma1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtIdioma2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(comboFase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnCadastrar)
                .addContainerGap(150, Short.MAX_VALUE))
        );

        setSize(new java.awt.Dimension(778, 500));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    public void validarPalavra(){
        int fase;
        fase = comboFase.getSelectedIndex();
        
        
        /*if (comboFase.getSelectedItem().equals("Fase 1"))
            fase = 1;
        else if (comboFase.getSelectedItem().equals("Fase 2"))
            fase = 2;
        else if (comboFase.getSelectedItem().equals("Fase 3"))
            fase = 3;*/
            
        String idioma1 = "", idioma2 = "";
        
        if(txtIdioma2.getText().equals("") || txtIdioma1.getText().equals(""))
            JOptionPane.showMessageDialog(rootPane,"Necessário preencher todos os campos.");
        else{
            Conexao con = new Conexao();
            Connection con2 = con.conexao();

            PreparedStatement comando;
            PreparedStatement comando2;

            try{
                comando = con2.prepareStatement("select * from palavra aux where aux.idioma1 ilike '" + txtIdioma1.getText() +"'");
                ResultSet rs = comando.executeQuery();
                

                
                while(rs.next()){
                    idioma1 = (String.valueOf(rs.getString("idioma1")));
                    //palavraIngles = (String.valueOf(rs.getString("ingles")));
                }
               
                comando2 = con2.prepareStatement("select * from palavra aux where aux.idioma2 ilike '" + txtIdioma2.getText() +"'");
                ResultSet rs2 = comando.executeQuery();  
                
                while(rs2.next()){
                    idioma2 = (String.valueOf(rs2.getString("idioma2")));
                }
                comando.close();
            }catch (SQLException ex) {
                Logger.getLogger(cadastrarPalavras.class.getName()).log(Level.SEVERE, null, ex);
            }

            if(idioma1.equals(txtIdioma1.getText()) || idioma2.equals(txtIdioma2.getText()))
                JOptionPane.showMessageDialog(rootPane,"Palavra já cadastrada anteriormente.");
            else
                //Inserção no banco
               try {

                comando = con2.prepareStatement("INSERT INTO palavra (idioma1,idioma2,fase) VALUES (?,?,?)");
                comando.setString(1, txtIdioma1.getText());
                comando.setString(2, txtIdioma2.getText());
                comando.setInt(3, fase+1);
                
                comando.execute();               
                JOptionPane.showMessageDialog(rootPane,"Palavra cadastrada com sucesso.");
       
                } catch (SQLException ex) {
                Logger.getLogger(cadastrarPalavras.class.getName()).log(Level.SEVERE, null, ex);
           }
        }
        
    }
    
    private void btnCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCadastrarActionPerformed
        validarPalavra();    
        txtIdioma1.setText(null);
        txtIdioma2.setText(null);
    }//GEN-LAST:event_btnCadastrarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(cadastrarPalavras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(cadastrarPalavras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(cadastrarPalavras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(cadastrarPalavras.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

       
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new cadastrarPalavras().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCadastrar;
    private javax.swing.JComboBox comboFase;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtIdioma1;
    private javax.swing.JTextField txtIdioma2;
    // End of variables declaration//GEN-END:variables
}
